/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.p10.miage.gestionnote.action;

import fr.p10.miage.gestionnote.form.AjouterControleActionForm;
import fr.p10.miage.gestionnote.form.ECAjouterCommentaireEtudiantActionForm;
import fr.p10.miage.gestionnote.model.CommentaireValidateur;
import fr.p10.miage.gestionnote.model.NotesValidateur;
import fr.p10.miage.gestionnote.modelservice.EcsIntervenantServices;
import java.text.Normalizer;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionRedirect;

/**
 *
 * @author charleshenzel
 */
public class ECAjouterCommentaireEtudiantAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "insert-commentaire-etudiant-success";
    private static final String ERROR = "insert-commentaire-etudiant-error";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        ECAjouterCommentaireEtudiantActionForm ajouterCommentaireForm = (ECAjouterCommentaireEtudiantActionForm) form;

        Map<String, String> HashMap = ajouterCommentaireForm.getHashMap();
        
        boolean valide = false;
        ActionMessages errors = new ActionMessages();
        if (HashMap != null) 
        {
                    Set listKeys = HashMap.keySet();
                    Iterator iterateur = listKeys.iterator();
                    
                    CommentaireValidateur commentaireValidateur= new CommentaireValidateur(HashMap);
                    valide = commentaireValidateur.validate(errors);
                    
                    if(!valide)
                    {
                        saveErrors(request.getSession(), errors);
                        ActionRedirect redirect = new ActionRedirect(mapping.findForward(ERROR));
                        redirect.addParameter("idec", ajouterCommentaireForm.getIdec());
                        redirect.addParameter("idp", ajouterCommentaireForm.getIdp());
                        return redirect;
                    }
                    
                    while (iterateur.hasNext()) 
                    {
                        boolean dispense=false;
                        
                        Object key = iterateur.next();
                        String commentaire =HashMap.get(key);
                        
                        if(!(String.valueOf(commentaire).isEmpty()))
                        {
                            EcsIntervenantServices.ajouterCommentaireEtudiant(
                                    ajouterCommentaireForm.getIdec(),
                                    Integer.parseInt(key.toString()),
                                    ajouterCommentaireForm.getIdp(),
                                    commentaire);
                        }
                        
                    }
        }

         ActionMessages messages = new ActionMessages();
            messages.add("succesInsertCommentaire", new ActionMessage("succes.commentaire"));
            saveMessages(request.getSession(), messages);

            ActionRedirect redirect = new ActionRedirect(mapping.findForward(SUCCESS));
            redirect.addParameter("idec", ajouterCommentaireForm.getIdec());
            redirect.addParameter("idp", ajouterCommentaireForm.getIdp());
            return redirect;
    }
}
